package com.zmn.plat.services.impl.standard.record;

import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.pager.LayuiPageUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.model.entity.standard.record.StandardRecord;
import com.zmn.plat.model.entity.standard.record.StandardRecordQuery;
import com.zmn.plat.model.vo.standard.record.StandardRecordExportExcelVO;
import com.zmn.plat.persistence.interfaces.standard.record.StandardRecordDao;
import com.zmn.plat.services.interfaces.standard.record.StandardRecordService;

@Service
public class StandardRecordServiceImpl implements StandardRecordService {

    @Resource
    StandardRecordDao standardRecordDao;

    @Override
    public StandardRecord findByKey(Integer standardId) {
        return standardRecordDao.findByKey(standardId);
    }

    @Override
    public List<StandardRecord> listPageByQuery(StandardRecordQuery query) {
        if (query.getCloseQueryTimeFilter() == null || !query.getCloseQueryTimeFilter()) {
            // 格式化时间选择器时间
            if (!StringUtil.isBlank(query.getStartAndEndDate())) {
                String[] strings = LayuiPageUtil.dateIntercept(query.getStartAndEndDate());
                query.setCreateTimeBegin(DateUtil.getDateStart(DateUtil.parse(strings[0], DateUtil.FORMAT_DATE)));
                query.setCreateTimeEnd(DateUtil.getDateEnd(strings[1], DateUtil.FORMAT_DATE));
            }
        }
        int count = countByQuery(query);
        query.setTotalCount(count);
        return count > 0 ? (standardRecordDao.listPageByQuery(query)) : Collections.emptyList();
    }

    @Override
    @SuppressWarnings("Duplicates")
    public List<StandardRecordExportExcelVO> listExportExcelPageByQuery(StandardRecordQuery query) {
        if (query.getCloseQueryTimeFilter() == null || !query.getCloseQueryTimeFilter()) {
            // 格式化时间选择器时间
            if (!StringUtil.isBlank(query.getStartAndEndDate())) {
                String[] strings = LayuiPageUtil.dateIntercept(query.getStartAndEndDate());
                query.setCreateTimeBegin(DateUtil.getDateStart(DateUtil.parse(strings[0], DateUtil.FORMAT_DATE)));
                query.setCreateTimeEnd(DateUtil.getDateEnd(strings[1], DateUtil.FORMAT_DATE));
            }
        }

        return standardRecordDao.listExportExcelPageByQuery(query);
    }

    @Override
    public List<StandardRecord> listPageByCheckQuery(StandardRecordQuery query) {
        // 格式化时间选择器时间
        if (StringUtil.isBlank(query.getStartAndEndDate())) {
            query.setStartAndEndDate(DateUtil.getStandardDateRangDeferDay(30).replaceAll("/", "-"));
        }

        String[] strings = LayuiPageUtil.dateIntercept(query.getStartAndEndDate());
        query.setPublishTimeBegin(DateUtil.getDateStart(DateUtil.parse(strings[0], DateUtil.FORMAT_DATE)));
        query.setPublishTimeEnd(DateUtil.getDateEnd(strings[1], DateUtil.FORMAT_DATE));

        int count = countByCheckQuery(query);
        query.setTotalCount(count);
        return count > 0 ? (standardRecordDao.listPageByCheckQuery(query)) : Collections.emptyList();
    }

    @Override
    public List<StandardRecord> listByQuery(StandardRecordQuery query) {
        return standardRecordDao.listByQuery(query);
    }

    @Override
    public List<StandardRecord> listAll() {
        return standardRecordDao.listAll();
    }

    @Override
    public List<StandardRecord> listByUnReadAndStaffId(Integer staffId) {
        return standardRecordDao.listByUnReadAndStaffId(staffId);
    }

    @Override
    public Integer countByQuery(StandardRecordQuery query) {
        return standardRecordDao.countByQuery(query);
    }

    @Override
    public Integer countByCheckQuery(StandardRecordQuery query) {
        return standardRecordDao.countByCheckQuery(query);
    }

    @Override
    public Integer updateStatusByKey(StandardRecord standardRecord) {
        return standardRecordDao.updateStatusByKey(standardRecord);
    }

    @Override
    public Integer updateStatusBatchByKey(List<Integer> ids, Integer status, String mcStaffName) {
        return standardRecordDao.updateBatchStatusByKey(ids, status, mcStaffName);
    }

    @Override
    public Integer updateSortByKey(StandardRecord standardRecord) {
        return standardRecordDao.updateSortByKey(standardRecord);
    }

    @Override
    public Integer updateSortByKey(Integer standardId, Integer sort, String mcStaffName) {
        StandardRecord standardRecord = new StandardRecord();
        standardRecord.setStandardId(standardId);
        standardRecord.setSort(sort);
        standardRecord.setUpdater(mcStaffName);

        return updateSortByKey(standardRecord);
    }

    @Override
    public Integer updateByKey(StandardRecord standardRecord) {
        return standardRecordDao.updateByKey(standardRecord);
    }

    @Override
    public Integer updateBaseInfoByKey(StandardRecord standardRecord) {
        return standardRecordDao.updateBaseInfoByKey(standardRecord);
    }

    @Override
    public Integer incrReadCount(Integer standardId) {
        return standardRecordDao.incrReadCount(standardId);
    }

    @Override
    public Integer insert(StandardRecord standardRecord) {
        return standardRecordDao.insert(standardRecord);
    }

    @Override
    public Integer deleteByKey(Integer standardId) {
        return standardRecordDao.deleteByKey(standardId);
    }

}